package com.mycomp.cm.service.impl;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mycomp.cm.dao.ContactDAO;
import com.mycomp.cm.dao.UserDAO;
import com.mycomp.cm.model.Contact;
import com.mycomp.cm.model.User;
import com.mycomp.cm.service.UserService;


@Service
public class UserServiceImpl implements UserService {

	private UserDAO userDAO;

	private ContactDAO contactDAO;

	@Autowired
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	@Autowired
	public void setContactDAO(ContactDAO contactDAO) {
		this.contactDAO = contactDAO;
	}

	
	public void register(User user) {
		// B/L
		userDAO.save(user);
	}


	public User login(String userName, String password) {

		User dbuser = userDAO.findByID(userName);

		if (dbuser != null) {

			if (dbuser.getPassword().equals(password)) {
				return dbuser;
			} else {
				return null;
			}

		} else {
			return null;
		}

	}

	
	public void addContact(Contact contact) {

		// B.L
		contactDAO.save(contact);

	}


	public void updateContact(Contact contact) {
		// B.L
		contactDAO.update(contact);
	}

	
	public void deleteContact(int contID) {
		contactDAO.delete(contID);
	}


	public Set<Contact> getUserContact(String userName) {
		return userDAO.findUserContacts(userName);
	}

}
